#ifndef CommInOut_H
#define CommInOut_H

#define T_SRESET 15
#define T_SRXCYC 500
#define T_SRXERR 180
#define T_SCOMCYC 1100
#define T_SCHGERR 120

#define RXDDATA 0x00
#define RXDDEAL 0x01
#define TXDDATA 0x02
#define TXDDEAL 0x03

#define ORDER81 0x81
#define ORDER82 0x82
#define ORDER8F 0x8F
#define ORDER91 0x91
#define ORDER92 0x92

#ifdef CommInOut_Variable
#define CommInOut_Extern

static U8 txdOrder; /* 发送命令字 */

static struct
{
    U8 status; /* 内外机通讯状态 */

    U8 *tx_address;
    U8 tx_count;

    U8 *rx_address;
    U8 rx_count;
    U8 rx_length;
} CommInOut;

#else
#define CommInOut_Extern extern
#endif

typedef struct
{
    U8 indoorNum;   /* 一拖几机型 */
    U8 run_mode;    /* 接收到的外机运行模式 */
    U8 comp_freq;   /* 接收到的外机压缩机频率 */
    U16 ofan_speed; /* 接收到的外机风机转速 */
    U16 eve_value;  /* 接收到的电子膨胀阀开度 */

    U16 ac_voltage; /* 接收到的外机交流电压 */
    U16 dc_voltage; /* 接收到的直流母线电压 */
    U16 ac_current; /* 接收到的外机交流电流 */
    U16 dc_current; /* 接收到的压缩机相电流 */
	U8 comp_type;   /* 压缩机品牌 */
	U8 ofan_type;   /* 外风机类型 */
	U8 capacity;
} parameter_out_typeDef;

typedef struct
{
    U8 noComm;     /* 无零火线通讯计时 */
    U8 tsreset;    /* 通讯上电复位计时 */
    U8 tsfrmwt;    /* 通讯超时计时 */
    U8 commErr;    /* 通讯故障计时 */
    U8 commRstErr; /* 通讯复位故障计时 */

    U16 commRun;    /* 通讯运行计时 */
    U16 txEndDelay; /* 通讯发送结束延时 */
} timer_CommInOut_typeDef;


extern parameter_out_typeDef parameter_out;
extern timer_CommInOut_typeDef timer_CommInOut;



CommInOut_Extern U8 slaveAddr;     /* 从机地址 */
CommInOut_Extern U8 count_GetAddr; /* 获取从机地址次数 */

CommInOut_Extern ram_byte CommInOutFlag1;
#define f_CompOn CommInOutFlag1.bit.b0      /* 压缩机开启 */
#define f_CompHeater CommInOutFlag1.bit.b1  /* 有压缩机电加热带 */
#define f_RoomUnitOn CommInOutFlag1.bit.b2  /* 系统开 */
#define f_RoomUnitOff CommInOutFlag1.bit.b3 /* 系统关 */
#define f_ExpExist CommInOutFlag1.bit.b4    /* 有电子膨胀阀 */
#define f_ExpRest CommInOutFlag1.bit.b5    /* 电子膨胀阀复位 */
#define f_4way CommInOutFlag1.bit.b6    /* 四通 */
#define f_HeaterOut CommInOutFlag1.bit.b7    /* 电加热 */

CommInOut_Extern ram_byte CommInOutFlag3;
#define f_ExpPlus CommInOutFlag3.bit.b0    /* 阀 */

CommInOut_Extern ram_byte CommInOutFlag4;
#define f_outfan CommInOutFlag4.bit.b0    /* 外风机 */
#define f_wait CommInOutFlag4.bit.b1    /* 待机 */
#define f_outquiet CommInOutFlag4.bit.b2    /* 外机静音 */
#define f_outCondensation CommInOutFlag4.bit.b3    /* 外机防凝露 */


CommInOut_Extern ram_byte CommInOutFlag2;
#define f_CommErr CommInOutFlag2.bit.b0      /* 通讯故障 */
#define f_OutCommErr CommInOutFlag2.bit.b1   /* 外机报通讯故障 */
#define f_Defrosting CommInOutFlag2.bit.b2   /* 化霜标志 */
#define f_TxdResetReq CommInOutFlag2.bit.b3  /* 通讯复位请求 */
#define f_CoolOilBack CommInOutFlag2.bit.b4  /* 制冷回油 */
#define f_HeatOilBack CommInOutFlag2.bit.b5  /* 制热回油 */
#define f_GetSlaveAddr CommInOutFlag2.bit.b6 /* 获取从机地址标志 */
#define f_skflg CommInOutFlag2.bit.b7

CommInOut_Extern U8 OUT_BOARD_CODING_0;
CommInOut_Extern U8 OUT_BOARD_CODING_1;
CommInOut_Extern U8 OUT_BOARD_CODING_2;
CommInOut_Extern U8 OUT_BOARD_CODING_3;
CommInOut_Extern U8 OUT_BOARD_CODING_4;

CommInOut_Extern U8 OUT_PROGRAM_CODING_0;
CommInOut_Extern U8 OUT_PROGRAM_CODING_1;
CommInOut_Extern U8 OUT_PROGRAM_CODING_2;
CommInOut_Extern U8 OUT_PROGRAM_CODING_3;

CommInOut_Extern U8 EEPROM_CODING_0;
CommInOut_Extern U8 EEPROM_CODING_1;
CommInOut_Extern U8 EEPROM_CODING_2;
CommInOut_Extern U8 EEPROM_CODING_3;

CommInOut_Extern U8 OUT_PROGRAM_VER;
CommInOut_Extern U8 EEPROM_VER;
CommInOut_Extern U8 Protocol_VER;
CommInOut_Extern U8 Valve_Close;

CommInOut_Extern ram_byte buff_InOut[20];
#define commOrder buff_InOut[0].byte
#define commAddr buff_InOut[1].byte

#define commFlag buff_InOut[2].byte
#define f_commFlag_b0 buff_InOut[2].bit.b0
#define f_commFlag_b1 buff_InOut[2].bit.b1
#define f_commFlag_b2 buff_InOut[2].bit.b2
#define f_commFlag_b3 buff_InOut[2].bit.b3
#define f_commFlag_b4 buff_InOut[2].bit.b4
#define f_commFlag_b5 buff_InOut[2].bit.b5
#define f_commFlag_b6 buff_InOut[2].bit.b6
#define f_commFlag_b7 buff_InOut[2].bit.b7

#define commData0 buff_InOut[3].byte
#define f_commData0_b0 buff_InOut[3].bit.b0
#define f_commData0_b1 buff_InOut[3].bit.b1
#define f_commData0_b2 buff_InOut[3].bit.b2
#define f_commData0_b3 buff_InOut[3].bit.b3
#define f_commData0_b4 buff_InOut[3].bit.b4
#define f_commData0_b5 buff_InOut[3].bit.b5
#define f_commData0_b6 buff_InOut[3].bit.b6
#define f_commData0_b7 buff_InOut[3].bit.b7

#define commData1 buff_InOut[4].byte
#define f_commData1_b0 buff_InOut[4].bit.b0
#define f_commData1_b1 buff_InOut[4].bit.b1
#define f_commData1_b2 buff_InOut[4].bit.b2
#define f_commData1_b3 buff_InOut[4].bit.b3
#define f_commData1_b4 buff_InOut[4].bit.b4
#define f_commData1_b5 buff_InOut[4].bit.b5
#define f_commData1_b6 buff_InOut[4].bit.b6
#define f_commData1_b7 buff_InOut[4].bit.b7

#define commData2 buff_InOut[5].byte
#define f_commData2_b0 buff_InOut[5].bit.b0
#define f_commData2_b1 buff_InOut[5].bit.b1
#define f_commData2_b2 buff_InOut[5].bit.b2
#define f_commData2_b3 buff_InOut[5].bit.b3
#define f_commData2_b4 buff_InOut[5].bit.b4
#define f_commData2_b5 buff_InOut[5].bit.b5
#define f_commData2_b6 buff_InOut[5].bit.b6
#define f_commData2_b7 buff_InOut[5].bit.b7

#define commData3 buff_InOut[6].byte
#define f_commData3_b0 buff_InOut[6].bit.b0
#define f_commData3_b1 buff_InOut[6].bit.b1
#define f_commData3_b2 buff_InOut[6].bit.b2
#define f_commData3_b3 buff_InOut[6].bit.b3
#define f_commData3_b4 buff_InOut[6].bit.b4
#define f_commData3_b5 buff_InOut[6].bit.b5
#define f_commData3_b6 buff_InOut[6].bit.b6
#define f_commData3_b7 buff_InOut[6].bit.b7

#define commData4 buff_InOut[7].byte
#define f_commData4_b0 buff_InOut[7].bit.b0
#define f_commData4_b1 buff_InOut[7].bit.b1
#define f_commData4_b2 buff_InOut[7].bit.b2
#define f_commData4_b3 buff_InOut[7].bit.b3
#define f_commData4_b4 buff_InOut[7].bit.b4
#define f_commData4_b5 buff_InOut[7].bit.b5
#define f_commData4_b6 buff_InOut[7].bit.b6
#define f_commData4_b7 buff_InOut[7].bit.b7

#define commData5 buff_InOut[8].byte
#define f_commData5_b0 buff_InOut[8].bit.b0
#define f_commData5_b1 buff_InOut[8].bit.b1
#define f_commData5_b2 buff_InOut[8].bit.b2
#define f_commData5_b3 buff_InOut[8].bit.b3
#define f_commData5_b4 buff_InOut[8].bit.b4
#define f_commData5_b5 buff_InOut[8].bit.b5
#define f_commData5_b6 buff_InOut[8].bit.b6
#define f_commData5_b7 buff_InOut[8].bit.b7

#define commData6 buff_InOut[9].byte
#define f_commData6_b0 buff_InOut[9].bit.b0
#define f_commData6_b1 buff_InOut[9].bit.b1
#define f_commData6_b2 buff_InOut[9].bit.b2
#define f_commData6_b3 buff_InOut[9].bit.b3
#define f_commData6_b4 buff_InOut[9].bit.b4
#define f_commData6_b5 buff_InOut[9].bit.b5
#define f_commData6_b6 buff_InOut[9].bit.b6
#define f_commData6_b7 buff_InOut[9].bit.b7

#define commData7 buff_InOut[10].byte
#define f_commData7_b0 buff_InOut[10].bit.b0
#define f_commData7_b1 buff_InOut[10].bit.b1
#define f_commData7_b2 buff_InOut[10].bit.b2
#define f_commData7_b3 buff_InOut[10].bit.b3
#define f_commData7_b4 buff_InOut[10].bit.b4
#define f_commData7_b5 buff_InOut[10].bit.b5
#define f_commData7_b6 buff_InOut[10].bit.b6
#define f_commData7_b7 buff_InOut[10].bit.b7

#define commData8 buff_InOut[11].byte
#define f_commData8_b0 buff_InOut[11].bit.b0
#define f_commData8_b1 buff_InOut[11].bit.b1
#define f_commData8_b2 buff_InOut[11].bit.b2
#define f_commData8_b3 buff_InOut[11].bit.b3
#define f_commData8_b4 buff_InOut[11].bit.b4
#define f_commData8_b5 buff_InOut[11].bit.b5
#define f_commData8_b6 buff_InOut[11].bit.b6
#define f_commData8_b7 buff_InOut[11].bit.b7

#define commData9 buff_InOut[12].byte
#define f_commData9_b0 buff_InOut[12].bit.b0
#define f_commData9_b1 buff_InOut[12].bit.b1
#define f_commData9_b2 buff_InOut[12].bit.b2
#define f_commData9_b3 buff_InOut[12].bit.b3
#define f_commData9_b4 buff_InOut[12].bit.b4
#define f_commData9_b5 buff_InOut[12].bit.b5
#define f_commData9_b6 buff_InOut[12].bit.b6
#define f_commData9_b7 buff_InOut[12].bit.b7

#define commData10 buff_InOut[13].byte
#define f_commData10_b0 buff_InOut[13].bit.b0
#define f_commData10_b1 buff_InOut[13].bit.b1
#define f_commData10_b2 buff_InOut[13].bit.b2
#define f_commData10_b3 buff_InOut[13].bit.b3
#define f_commData10_b4 buff_InOut[13].bit.b4
#define f_commData10_b5 buff_InOut[13].bit.b5
#define f_commData10_b6 buff_InOut[13].bit.b6
#define f_commData10_b7 buff_InOut[13].bit.b7

#define commData11 buff_InOut[14].byte
#define f_commData11_b0 buff_InOut[14].bit.b0
#define f_commData11_b1 buff_InOut[14].bit.b1
#define f_commData11_b2 buff_InOut[14].bit.b2
#define f_commData11_b3 buff_InOut[14].bit.b3
#define f_commData11_b4 buff_InOut[14].bit.b4
#define f_commData11_b5 buff_InOut[14].bit.b5
#define f_commData11_b6 buff_InOut[14].bit.b6
#define f_commData11_b7 buff_InOut[14].bit.b7

#define commData12 buff_InOut[15].byte
#define f_commData12_b0 buff_InOut[15].bit.b0
#define f_commData12_b1 buff_InOut[15].bit.b1
#define f_commData12_b2 buff_InOut[15].bit.b2
#define f_commData12_b3 buff_InOut[15].bit.b3
#define f_commData12_b4 buff_InOut[15].bit.b4
#define f_commData12_b5 buff_InOut[15].bit.b5
#define f_commData12_b6 buff_InOut[15].bit.b6
#define f_commData12_b7 buff_InOut[15].bit.b7

#define commData13 buff_InOut[16].byte
#define f_commData13_b0 buff_InOut[16].bit.b0
#define f_commData13_b1 buff_InOut[16].bit.b1
#define f_commData13_b2 buff_InOut[16].bit.b2
#define f_commData13_b3 buff_InOut[16].bit.b3
#define f_commData13_b4 buff_InOut[16].bit.b4
#define f_commData13_b5 buff_InOut[16].bit.b5
#define f_commData13_b6 buff_InOut[16].bit.b6
#define f_commData13_b7 buff_InOut[16].bit.b7

#define commData14 buff_InOut[17].byte
#define f_commData14_b0 buff_InOut[17].bit.b0
#define f_commData14_b1 buff_InOut[17].bit.b1
#define f_commData14_b2 buff_InOut[17].bit.b2
#define f_commData14_b3 buff_InOut[17].bit.b3
#define f_commData14_b4 buff_InOut[17].bit.b4
#define f_commData14_b5 buff_InOut[17].bit.b5
#define f_commData14_b6 buff_InOut[17].bit.b6
#define f_commData14_b7 buff_InOut[17].bit.b7

#define commData15 buff_InOut[18].byte
#define f_commData15_b0 buff_InOut[18].bit.b0
#define f_commData15_b1 buff_InOut[18].bit.b1
#define f_commData15_b2 buff_InOut[18].bit.b2
#define f_commData15_b3 buff_InOut[18].bit.b3
#define f_commData15_b4 buff_InOut[18].bit.b4
#define f_commData15_b5 buff_InOut[18].bit.b5
#define f_commData15_b6 buff_InOut[18].bit.b6
#define f_commData15_b7 buff_InOut[18].bit.b7

#define commCheck buff_InOut[19].byte

CommInOut_Extern ram_byte OutSafeErrFlag[9];
#define OutSafeErr0 OutSafeErrFlag[0].byte
#define f_OutErr_ALL OutSafeErrFlag[0].bit.b0
//#define f_OutErr_END OutSafeErrFlag[0].bit.b1

#define OutSafeErr1 OutSafeErrFlag[1].byte

/*
#define f_OutErr_F0 OutSafeErrFlag[1].bit.b0
#define f_OutErr_F1 OutSafeErrFlag[1].bit.b1
#define f_OutErr_F2 OutSafeErrFlag[1].bit.b2
#define f_OutErr_F3 OutSafeErrFlag[1].bit.b3
#define f_OutErr_F4 OutSafeErrFlag[1].bit.b4
#define f_OutErr_F5 OutSafeErrFlag[1].bit.b5
#define f_OutErr_F6 OutSafeErrFlag[1].bit.b6
#define f_OutErr_F7 OutSafeErrFlag[1].bit.b7
*/


#define OutSafeErr2 OutSafeErrFlag[2].byte
#define f_OutErr_H0 OutSafeErrFlag[2].bit.b0
#define f_OutErr_H1 OutSafeErrFlag[2].bit.b1
#define f_OutErr_H2 OutSafeErrFlag[2].bit.b2
#define f_OutErr_H3 OutSafeErrFlag[2].bit.b3
//#define f_OutErr_H4 OutSafeErrFlag[2].bit.b4
//#define f_OutErr_H5 OutSafeErrFlag[2].bit.b5
//#define f_OutErr_H6 OutSafeErrFlag[2].bit.b6
#define f_OutErr_H7 OutSafeErrFlag[2].bit.b7

#define OutSafeErr3 OutSafeErrFlag[3].byte
#define f_OutErr_H8 OutSafeErrFlag[3].bit.b0
//#define f_OutErr_H9 OutSafeErrFlag[3].bit.b1

/*
#define f_OutErr_HA OutSafeErrFlag[3].bit.b2
#define f_OutErr_Hb OutSafeErrFlag[3].bit.b3
#define f_OutErr_HC OutSafeErrFlag[3].bit.b4
#define f_OutErr_Hd OutSafeErrFlag[3].bit.b5
#define f_OutErr_HE OutSafeErrFlag[3].bit.b6
#define f_OutErr_HF OutSafeErrFlag[3].bit.b7
*/

#define OutSafeErr4 OutSafeErrFlag[4].byte
#define f_OutErr_L0 OutSafeErrFlag[4].bit.b0
#define f_OutErr_L1 OutSafeErrFlag[4].bit.b1
#define f_OutErr_L2 OutSafeErrFlag[4].bit.b2
#define f_OutErr_L3 OutSafeErrFlag[4].bit.b3
#define f_OutErr_L4 OutSafeErrFlag[4].bit.b4
#define f_OutErr_L5 OutSafeErrFlag[4].bit.b5
#define f_OutErr_L6 OutSafeErrFlag[4].bit.b6
#define f_OutErr_L7 OutSafeErrFlag[4].bit.b7




#define OutSafeErr5 OutSafeErrFlag[5].byte
#define f_OutErr_L8_0 OutSafeErrFlag[5].bit.b0
#define f_OutErr_L8_1 OutSafeErrFlag[5].bit.b1
#define f_OutErr_L8_2 OutSafeErrFlag[5].bit.b2
#define f_OutErr_L8_3 OutSafeErrFlag[5].bit.b3
#define f_OutErr_L8_4 OutSafeErrFlag[5].bit.b4
#define f_OutErr_L9 OutSafeErrFlag[5].bit.b5
//#define f_OutErr_LA OutSafeErrFlag[5].bit.b6
//#define f_OutErr_Ld OutSafeErrFlag[5].bit.b7

#define OutSafeErr6 OutSafeErrFlag[6].byte
#define f_OutErr_U0 OutSafeErrFlag[6].bit.b0
#define f_OutErr_U1 OutSafeErrFlag[6].bit.b1
#define f_OutErr_U2 OutSafeErrFlag[6].bit.b2
#define f_OutErr_U3 OutSafeErrFlag[6].bit.b3
#define f_OutErr_U4 OutSafeErrFlag[6].bit.b4
#define f_OutErr_U5 OutSafeErrFlag[6].bit.b5
#define f_OutErr_U6 OutSafeErrFlag[6].bit.b6
#define f_OutErr_U7 OutSafeErrFlag[6].bit.b7  

#define OutSafeErr7 OutSafeErrFlag[7].byte
#define f_OutErr_U8 OutSafeErrFlag[7].bit.b0
#define f_OutErr_U9 OutSafeErrFlag[7].bit.b1
#define f_OutErr_UA OutSafeErrFlag[7].bit.b2
#define f_OutErr_Ub OutSafeErrFlag[7].bit.b3
#define f_OutErr_UC OutSafeErrFlag[7].bit.b4
#define f_OutErr_Ud OutSafeErrFlag[7].bit.b5
#define f_OutErr_UE OutSafeErrFlag[7].bit.b6
#define f_OutErr_UF OutSafeErrFlag[7].bit.b7

#define OutSafeErr8 OutSafeErrFlag[8].byte
#define f_OutErr_E2 OutSafeErrFlag[8].bit.b0
#define f_OutErr_E3 OutSafeErrFlag[8].bit.b1
#define f_OutErr_E4 OutSafeErrFlag[8].bit.b2
#define f_OutErr_E5 OutSafeErrFlag[8].bit.b3
//#define f_OutErr_E6 OutSafeErrFlag[8].bit.b4
//#define f_OutErr_E7 OutSafeErrFlag[8].bit.b5
#define f_OutErr_E8 OutSafeErrFlag[8].bit.b6
//#define f_OutErr_E9 OutSafeErrFlag[8].bit.b7

void CommInOut_Tx(U8 *, U8);
void CommInOut_Tx_Intp(void);
void CommInOut_Rx(U8 *, U8);
void CommInOut_Rx_Intp(U8);

void CommInOut_Act(void);

U8 CommGetBCC(void);
U8 CommGetBCC_SK(void);

void CommBuffClr(void);

void RxdData(void);
U8 RxdCheck(void);
void RxdOrder01(void);
void RxdOrder02(void);

void RxdOrder11(void);
void RxdOrder12(void);

void TxdData(void);
void TxdOrder81(void);
void TxdOrder82(void);
void TxdOrder8F(void);
void TxdOrder91(void);
void TxdOrder92(void);

void CommInOut_timer_500us(void);
void CommInOut_time_1s(void);
void CommErrCheck(void);
#endif
